﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using YLPublicService.TransferModel;
using Util;

namespace YLPublicService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public partial class UserService : ServiceBase<User>, IUserService
    {
        static UserService()
        {
        }


        public override void LoginOut(Guid guid)
        {
            disableUserOnFtp(getCurrentUser(guid).LoginName);//loginout后当前登录guid 就没了，所以要提前调用
            base.LoginOut(guid);
        }

        //返回ftp 地址 密码等

        /// <summary>
        /// 获取帐户余额
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public TransferModel.UserAccount GetAccount(Guid guid)
        {
            if (!isOnline(guid))
            {
                return null;
            }
            int uid = getCurrentUser(guid).UserID;
            dbContext.Refresh(RefreshMode.OverwriteCurrentValues, dbContext.UserAccounts);
            var userAccount = dbContext.UserAccounts.Where(ua => ua.UserID == uid).First();
            TransferModel.UserAccount tua = new TransferModel.UserAccount() { UserID = userAccount.UserID, Money = userAccount.Money };
            return tua;
        }

        //private IEnumerable<TransferModel.WorkItem> getWorkItems(Func<View_UserAndItem, bool> predicate, Guid guid)
        //{
        //    if (!isOnline(guid))
        //    {
        //        return null;
        //    }

        //    User user = getCurrentUser(guid);
        //    var items = dbContext.View_UserAndItems
        //        .Where(predicate)
        //        .Select(ui =>
        //                new TransferModel.WorkItem
        //                {
        //                    WorkItemID = ui.ItemID,
        //                    TemplateID = ui.TemplateID,
        //                    Detial = ui.Detial,
        //                    Status = ui.Status,
        //                    StatusDescription = ui.discription,
        //                    DBTime = ui.DBTime
        //                }
        //            );
        //    return items;
        //}





        /// <summary>
        /// 用户名，密码正确，且帐户启用
        /// 成功则返回用户对象
        /// 注意：非数据库中的用户对象类型
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="pwd"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        protected override bool TryFindUser(string loginName, string pwd, out User user)
        {
            var ret = from u in dbContext.Users //.Where(su => su.LoginName == user.LoginName).First();
                      where u.LoginName == loginName && u.PassWord == new Binary(HashAlgorithm.getHex(pwd))
                                   && u.Enabled == true
                      select u;
            if (ret.Count() > 0)
            {
                user = ret.First();
                return true;
            }
            else
            {
                user = null;
                return false;
            }
        }


        //public TransferModel.Template[] GetEnableTemplates(Guid guid)
        //{
        //    if (!isOnline(guid))
        //    {
        //        return null;
        //    }
        //    var tnames = from t in dbContext.TemplatesV2s
        //                 where t.Enable == 1
        //                 select TransferModel.Template.FromXML(t.Detial);
        //    return tnames.ToArray();
        //}


        public string GetFinishFileServerPath(int workItemID, Guid guid)
        {
            if (!isOnline(guid))
            {
                return "";
            }

            User user = getCurrentUser(guid);
            var x = from ui in dbContext.View_FinishWorkItems
                    where ui.UserID == user.UserID && ui.ItemID == workItemID
                    select ui;

            if (x.Count() == 1)
            {
                var item = x.First();
                var dir = item.PhotoServerRelativePath;
                var exeName = TemplateItem.TemplateDetial.FromXML(item.Detial).mName + ".exe";
                return global::Common.conbindPath(dir, exeName);
            }
            else
            {
                throw new Exception("服务端文件路径出错。");
            }
        }




    }
}
